import random
import requests
import mymd5
import json

if __name__ == '__main__':
    try:
        while True:
            print("请输入(中文或英文):", end=' ')
            instr = input()
            if len(instr) <= 0:
                continue
            first = str(instr[0])
            first = first.lower()
            if first >= 'a' and first <= 'z':
                fromLanguage = 'en'
                toLanguage = 'zh'
            else:
                fromLanguage = 'zh'
                toLanguage = 'en'
            url = "http://api.fanyi.baidu.com/api/trans/vip/translate"
            ran = str(int(random.random() * 10000000000))
            body = {'q': instr, 'appid': '20151126000006681', 'salt': ran,
                    'from': fromLanguage, 'to': toLanguage, "sign": mymd5.makemd5(instr, ran)}
            r = requests.get(url, params=body)
            text = r.text
            jsontext = json.loads(text)
            print(jsontext['trans_result'][0]['dst'])
    except (EOFError, KeyboardInterrupt):
        print()
        exit()
